unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UMono, UMonoIntf, UMonoRTTIInv, UScriptClass, UVarMono, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Test;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not Assigned(MonoJit) then
    exit;

  Test;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown:= DebugHook <> 0;
  SetMonoPath('C:\Program Files\Mono-2.8.2\bin');
  InitMono;
end;

procedure myShowMessage(aValue: PMonoString); cdecl;
var
  sw: string;
begin
  sw := TMonoConvert.MonoStrToString(aValue);
  Dialogs.ShowMessage('MONO: '+sw);
end;

function myInputBox(aCaption: PMonoString; aPrompt: PMonoString; aDefault: PMonoString): PMonoString; cdecl;
var
  s: string;
begin
  s := InputBox(TMonoConvert.MonoStrToString(aCaption),TMonoConvert.MonoStrToString(aPrompt), TMonoConvert.MonoStrToString(aDefault));
  result := TMonoConvert.StringToMonoStr(s);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(MonoJit);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    MonoJit := TMonoJit.Create('v4.0.30319', OpenDialog1.FileName, '0', 0);

    MonoJit.AddInternalCall('Mono4Delphi','Dialogs','ShowMessage', @myShowMessage);
    MonoJit.AddInternalCall('Mono4Delphi','TProxyAnyIntf', 'DelphiInvoke', @DelphiRTTIInvoke);
    MonoJit.AddInternalCall('Mono4Delphi','MainClass','InputBox', @myInputBox);
    MonoJit.AddClassMap(TObject, 'Mono4Delphi.ITObject');
    MonoJit.AddClassMap(TComponent, 'Mono4Delphi.ITComponent');
    MonoJit.AddClassMap(TControl, 'Mono4Delphi.ITControl');
    MonoJit.AddClassMap(TForm, 'Mono4Delphi.ITForm');
    MonoJit.AddClassMap(TEdit, 'Mono4Delphi.ITEdit');
    MonoJit.AddClassMap(TButton, 'Mono4Delphi.ITButton');
    MonoJit.AddClassMap(TStrings, 'Mono4Delphi.ITStrings');
    MonoJit.AddClassMap(TMemo, 'Mono4Delphi.ITMemo');

    MonoJit.LoadAssembly;
  end;
end;

procedure TForm1.Test;
var
  vObj: variant;
  v: variant;
  s: string;
begin
  vObj := VarMonoCreate(MonoJit.FindMonoClass('Mono4Delphi', 'MainClass'), 'Mono4Delphi.ITForm', [self]);
  vObj.ShowCaption();

  s := vObj.Func1(10, 'alfa', 23.1, false);
  memo1.Lines.Add(s);
  s := vObj.Func4(varArrayOf([10, 'alfa', 23.1, false]));
  memo1.Lines.Add(s);

  v := VarArrayCreate([0,2], varOleStr);
  v[0] := '$01';
  v[1] := '$02';
  v[2] := '$03';
  vObj.Func3(v);
  memo1.Lines.Add(v[0]+v[1]+v[2]);

end;

end.
